package edu.usm.predatorwars;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import edu.predatorwars.shared.models.CharacterListPayload;
import edu.predatorwars.shared.models.Creature;
import edu.predatorwars.shared.models.GameInfo;
import edu.predatorwars.shared.models.GameListPayload;
import edu.predatorwars.shared.models.Move;
import edu.predatorwars.shared.models.Player;
import edu.predatorwars.shared.models.StateChangePayload;

/**
 * This is a stub implementation of {#link {@link GameHostResourceClient}
 * It is intended to simulate responses from the Game Host for testing purposes
 */
public class StubServer extends GameEventProducer implements GameHostResourceClient {

	private Map<UUID, GameInfo> games = new HashMap<UUID, GameInfo>();
	private List<Player> players = new ArrayList<Player>();
	private GameInfo currentGame;

	public StubServer(GameEventConsumer consumer) {
		super(consumer);
	}

	public Boolean createGame(Player arg0) {
		UUID uuid = UUID.randomUUID();
		GameInfo info = new GameInfo(uuid, 3, 0, "moutain");
		games.put(uuid, info);
		return true;
	}

	public boolean joinGame(Player arg0, UUID arg1) {
		if (!games.containsKey(arg1)) {
			return false;
		}
		currentGame = games.get(arg1);
		// Simulate the server accepting the player
		// queue a state change then a CharacterListPayload
		queueStateChange(1);
		queueCharacterList();
		return true;
	}

	private void queueStateChange(int i) {
		StateChangePayload stateChange = new StateChangePayload(i, players, currentGame.getGameId());
		consumer.addMessage(stateChange);
	}

	private void queueCharacterList() {
		List<Creature> creatures = getCreatureList();
		CharacterListPayload payload = 
			new CharacterListPayload(creatures.toArray(new Creature[0]), UUID.randomUUID());
		consumer.addMessage(payload);
	}

	private List<Creature> getCreatureList() {
		List<Creature> creatures = new ArrayList<Creature>();
		
		Creature creature = new Creature(Creature.TYPE_ELEPHANT, makeStats(10F, 3F, 2F, 5F));
		creatures.add(creature);
		
		creature = new Creature(Creature.TYPE_SNAKE, makeStats(3F, 10F, 8F, 3F));
		creatures.add(creature);
		
		return creatures;
	}

	private Map<Integer, Float> makeStats(float armor, float sight, float speed, float strength) {
		Map<Integer, Float> stats = new HashMap<Integer, Float>();
		stats.put(Creature.STAT_ARMOR, armor);
		stats.put(Creature.STAT_HEALTH, 100.0F);
		stats.put(Creature.STAT_MAX_HEALTH, 100.0F);
		stats.put(Creature.STAT_SIGHT, sight);
		stats.put(Creature.STAT_SPEED, speed);
		stats.put(Creature.STAT_STRENGTH, strength);
		return stats;
	}

	public boolean makeMovement(Player arg0, Move arg1, UUID arg2) {
		return false;
	}

	public boolean quitGame(Player arg0, UUID arg1) {
		return false;
	}

	public boolean selectCharacter(Player arg0, Creature arg1, UUID arg2) {
		queueStateChange(2);
		return true;
	}

	public void start() {
		
	}

	public boolean makeMovement(Player arg0, Move arg1, UUID arg2, UUID arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	public GameListPayload refreshGameList() {
		Collection<GameInfo> gameList = games.values();
		return new GameListPayload(new ArrayList<GameInfo>(gameList));
	}
}